//
//  AppDelegate.m
//  MoRan
//
//  Created by MoRan on 2022.
//  Copyright © 2022 MoRan. All rights reserved.
//

#import "AppDelegate.h"

#import "ApplicationManager.h"
#import <WebKit/WebKit.h>

@interface AppDelegate ()

@property (strong, nonatomic) WKWebView *wkWebView;
@property (nonatomic, strong) Reachability *internetReachability;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    _netStatus = ReachableViaWiFi;
            
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isFirst"] boolValue]) {
        //获取系统的UA
        [self getUA];
    }
    
    //网络监测
    [self addReachability];
        
    [kIAPHelper addTransactionObserver];
    
    //设置SVProgressHUD显示的样式
    [self setSVProgressHUD];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = kViewBackgroundColor;
    
    [kApplicationManager setupWindow:self.window];
    
    // 强制关闭暗黑模式
    #if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
    if(@available(iOS 13.0,*)){
        self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    #endif
    
    [kDataManager initTable];
    
    return YES;
}

-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
    if (notificationSettings.types == 0) {
        // 通知权限授权失败
    } else {
        // 通知权限授权成功
    }
}

/**获取系统的UA */
- (void)getUA {
    self.wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero];
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                                 cachePolicy:NSURLRequestUseProtocolCachePolicy
                                             timeoutInterval:10.f];
    [self.wkWebView loadRequest:request];
    [self.wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
        //1）获取默认userAgent：
        NSString *ua = result;   //直接获取为nil
        if (ua.length > 0) {
            UserManager *userManager = kUserManager.userManager;
            userManager.ua = ua;
            [userManager saveUserData:userManager];
        }
    }];
}

#pragma mark - 设置SVProgressHUD显示的样式

//SVProgressHUDMaskType 设置显示的样式
- (void)setSVProgressHUD {
    //SVProgressHUDMaskType 设置显示的样式
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];//设置HUD的Style
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];//设置HUD和文本的颜色
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];//设置HUD的背景颜色
    [SVProgressHUD setMinimumDismissTimeInterval:1];//设置显示停留时间
}

#pragma mark - 工程名

- (NSString *)getWorkspaceName {
    return @"MoRan";
}

#pragma mark -  网络监测

- (void)addReachability {
    //添加一个系统通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    //初始化
    self.internetReachability = [Reachability reachabilityForInternetConnection];
    //通知添加到Run Loop
    [self.internetReachability startNotifier];
    [self updateInterfaceWithReachability:_internetReachability];
}

- (void)reachabilityChanged:(NSNotification *)note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    [self updateInterfaceWithReachability:curReach];
}
 
- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
    _netStatus = [reachability currentReachabilityStatus];
    switch (_netStatus) {
        case NotReachable:
            NSLog(@"====当前网络状态不可达=======");
            break;
        case ReachableViaWiFi:
            NSLog(@"====当前网络状态为Wifi=======");
            break;
        case ReachableViaWWAN:
            NSLog(@"====当前网络状态为3G=======keso");
            break;
    }
}

@end
